import axios from 'axios'
import router from '../router/index'
// 进度条
import 'ant-design-vue/dist/antd.css';
import commmon from '../utils/common.js'
import qs from 'qs'
export default {
    install: function (vue) {

        var result = axios.create({
            baseURL: "http://1.117.91.88:52016/fraud-prevention/api" //Mock 不用计较地址
        })

        result.interceptors.request.use(function (request) {

            // if (request.method === 'get') {
            //     // 如果是get请求，且params是数组类型如arr=[1,2]，则转换成arr=1&arr=2,不转换会显示为arr[]=1&arr[]=2
            //     request.paramsSerializer = function (params) {
            //         return qs.stringify(params, { arrayFormat: 'repeat' })
            //     }
            // }
            // 不是验证码请求 不用携带token
            if (request.url != "/admin/captcha" && request.url != "/admin/login") {
                console.log("authorization = ", window.localStorage.getItem('authorization'))
                request.headers.Authorization = window.localStorage.getItem('authorization')
            }
            return request
        })
        // 返回400 提示权限不够 并跳转回主页面
        result.interceptors.response.use(
            response => {
                // switch (response.response.status) {
                // case 200:
                // console.log(response.response.data.msg)
                // }
                // console.log(response.data.msg)
                if (response.data.code != 200) { //如果内部的状态码不是200
                    console.log(response.data.msg)
                    commmon.notify("warning", response.data.msg.toString())
                }
                return response;
            },
            error => {
                if (error.response) {
                    switch (error.response.status) {
                        case 401:
                            console.log("status", error.response.status)
                            window.localStorage.setItem("authorization", "")
                            // 返回 401 清除token信息并跳转到登录页面
                            router.push({
                                path: '/login',
                            })
                            break;
                        case 400:
                            router.push({
                                path: '/home',
                            })
                            break;
                    }

                    commmon.notify("warning", error.response.data.msg)


                }
                return Promise.reject(error.response.data) // 返回接口返回的错误信息
            });
        vue.prototype.$http = result
    }
}